package com.xhui.vimarket.zkplus.utils.excel;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.swing.filechooser.FileSystemView;

/**
 * .
 * 
 * @版权：福富软件 版权所有 (c) 2011
 * @author wuyx
 * @version Revision 1.0.0
 * @see:
 * @创建日期：2011-2-12
 * @功能说明：
 * 
 */
public class ExcelDemo {
    
    /**
     * The main method.  Gets the worksheet and then uses the API 
     * within a simple loop to print out the spreadsheet contents as
     * comma separated values
     * 
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // excel
        Date dat = new Date();
        //dat.setTime(dat.getTime());               
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String timeStr = sdf.format(dat);
        FileSystemView fsv = FileSystemView.getFileSystemView();
        fsv.getHomeDirectory();
        String ps = fsv.getHomeDirectory() + "//" + timeStr + "" + "demo" + ".xls";
        File f = new File(ps);
        // Sheet
        Sheet sheet = new Sheet();
        sheet.setName("属性模板");
        // add to row
        Row rowa = new Row(); // attrId
        Row rowb = new Row(); // javaCode
        Row rowc = new Row(); // 类型
        Row rowd = new Row(); // 取值说明
        Row rowe = new Row(); // 名称
        // add def row
        addColToRow(rowa, "模板请勿修改", 0);
        
        addColToRow(rowb, "", 0);
        addColToRow(rowc, "", 0);
        addColToRow(rowd, "取值说明", 0);
        addColToRow(rowe, "本行开始请修改", 0);
        // add tmpl to row
        sheet.appendRow(rowa);
        sheet.appendRow(rowb);
        sheet.appendRow(rowc);
        sheet.appendRow(rowd);
        sheet.appendRow(rowe);
        List<Sheet> list = new ArrayList<Sheet>();
        list.add(sheet);
        try {
            Excel.write(list, new FileOutputStream(f));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    /**
     * addColToRow.
     * 
     * @param row
     *            Row
     * @param label
     *            String
     * @param colNum
     *            int
     * @author wuyx
     *         2011-2-11 wuyx
     */
    public static void addColToRow(Row row, String label, int colNum) {
        List rageList = new ArrayList();
        rageList.add("asdf");
        rageList.add("asd2f");
        
        List<Column> columnList = row.getColumnList() == null ? new ArrayList<Column>()
            : row.getColumnList();
        Column column = new Column();
        column.setColumnLabel(label);
        column.setColumnNum(0);
        if (label == null || "".equals(label)) {
            column.setDataValidationRange(rageList);
        }
        columnList.add(column);
        row.setColumnList(columnList);
    }
    
}
